package com.flyme.module.base.provider.controller;

import com.flyme.common.core.aliyun.sms.AliSmsUtil;
import com.flyme.common.core.aliyun.sms.SmsTempEnum;
import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.security.OpenHelper;
import com.flyme.common.core.security.OpenUserAuth;
import com.flyme.core.base.supper.controller.BaseController;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.module.base.client.constants.BaseConstants;
import com.flyme.module.base.client.dto.SysUserDto;
import com.flyme.module.base.client.entity.SysUser;
import com.flyme.module.base.provider.service.SysAccountService;
import com.flyme.module.base.provider.service.SysDeptService;
import com.flyme.module.base.provider.service.SysRoleService;
import com.flyme.module.base.provider.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * hr管理
 *
 * @author zyf
 */
@Api(tags = "hr管理")
@RestController
public class SysHrController extends BaseController<SysUserService, SysUser> {

    @Autowired
    private SysAccountService sysAccountService;
    @Autowired
    private SysRoleService roleService;
    @Autowired
    private SysDeptService deptService;
    @Autowired
    private SysUserService userService;

    /**
     * hr列表
     *
     * @return
     */
    @ApiOperation(value = "hr列表", notes = "hr列表")
    @PostMapping("/user/hrlist")
    public ResultBody list(@ModelAttribute PagerInfo pagerInfo, @ModelAttribute SysUser user) {
        OpenUserAuth userAuth = OpenHelper.getUserAuth();
        CriteriaQuery cq = new CriteriaQuery<EntityMap>(pagerInfo);
        cq.addSql("u.id,userName,avatar,mobile,positionName,hiredate,status,u.createTime");
        //cq.addSql("(select count(*) from sys_user user where user.deptId=u.deptId and user.userType='UT_STUDENT') userCount");
        cq.addSql("(select count(id) from sys_complan where hrId = u.id and NOW()>starTime and NOW()<endTime) underway");//获取进行中计划数量
        cq.addSql("(select count(id) from sys_complan where hrId = u.id and NOW()>endTime) finish");//获取已完成计划数量
        cq.like("userName", user.getUserName());
        cq.in("userType", "UT_COMPANY");
        cq.eq("companyId", userAuth.getCompanyId());
        cq.ne("u.id", userAuth.getUserId());
        cq.groupBy("id");
        PqGrid pqGrid = bizService.pageList(cq);
        List<EntityMap> list = (List<EntityMap>) pqGrid.getRows();
        for(EntityMap hr : list){
            hr.put("userCount", 0);
            List<Long> deptIds = deptService.getDeptId(hr.get("id"));
            if(deptIds.size() > 0){
                Integer num = userService.countByDept(deptIds);
                hr.put("userCount", num);
            }
            String deptNames = deptService.getDeptName(hr.get("id"));
            hr.put("deptName", deptNames);
        }
        pqGrid.setRows(list);
        return ResultBody.success(pqGrid);
    }

    /**
     * 添加hr
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "添加hr", notes = "添加hr")
    @PostMapping("/user/addhr")
    public ResultBody addHr(@ModelAttribute SysUserDto user) {
        OpenUserAuth userAuth = OpenHelper.getUserAuth();
        user.setCompanyId(userAuth.getCompanyId());
        user.setUserType("UT_COMPANY");
        user.setRoleCode("hr");
        user.setAvatar(BaseConstants.DEFAULT_USERHEAD);
        String mobile = user.getMobile();
        user.setPassword(mobile.substring(mobile.length() - 6));
        Boolean tag = sysAccountService.register(user);
        if (tag) {
            //发送短信通知
            Map<String, String> params = new HashMap<>();
            params.put("userName", user.getUserName());
            params.put("password", mobile.substring(mobile.length() - 6));
            AliSmsUtil.sendSmsCode(SmsTempEnum.REGHR, mobile, params);
        }
        return ResultBody.success("添加成功").setData(tag);

    }


    /**
     * 更新hr
     *
     * @param user
     * @return
     */
    @ApiOperation(value = "更新hr", notes = "更新hr")
    @PostMapping("/user/edithr")
    public ResultBody edithr(@ModelAttribute SysUser user) {
        Boolean tag = bizService.saveOrUpdate(user);
        return ResultBody.success("更新成功").setData(tag);
    }

    /**
     * 删除hr
     */
    @ApiOperation(value = "删除hr", notes = "删除hr")
    @PostMapping(value = "/user/delHrByIds")
    public ResultBody delByIds(@RequestParam Long[] ids) {
        Boolean tag = bizService.delByIds(ids);
        return ResultBody.success(tag);
    }


}
